import Vue from 'vue'
import VueRouter from 'vue-router'
import { project } from './project'
import Router from 'vue-router'
import NProgress from 'nprogress'

//  重写路由的push方法--->这个是vue-cli4.x以上的坑
const routerPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return routerPush.call(this, location).catch((error) => error)
}
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Index',
    component: () => import('@/views/index/index.vue'),
  },
  {
    path: '/index',
    name: 'Index',
    component: () => import('@/views/index/index.vue'),
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/login/login.vue'),
  },
  {
    path: '/register',
    name: 'Register',
    component: () => import('@/views/login/register'),
  },
  {
    path: '/register',
    name: 'Register',
    component: () => import('@/views/login/register'),
  },
  {
    path: '/resetPassword',
    name: 'ResetPassword',
    component: () => import('@/views/login/resetPasssword'),
  },
  {
    path: '/article',
    name: 'Article',
    component: () => import('@/views/article/article'),
  },
]

const router = new VueRouter({
  mode: 'hash',
  base: process.env.BASE_URL,
  routes: [...project, ...routes],
})
// 全局路由守卫
router.beforeEach((to, from, next) => {
  NProgress.start()
  let isLogin = sessionStorage.getItem('loginStatus') === 'true' // 判断是否登录，本地存储有用户数据则视为已经登录
  console.log('isLogin :>> ', isLogin, sessionStorage.getItem('loginStatus'))
  // if (isLogin) {
  //   if (to.path === '/login') {
  //     next({ path: '/' })
  //   }
  //   next()
  // } else {
  //   if (to.path === '/login') {
  //     next()
  //   } else if (to.path === '/register') {
  //     next()
  //   } else {
  //     next({ path: '/login' })
  //   }
  // }

  next()

  // if (to.path === '/') {
  //   next({ path: '/Innovation' })
  // } else {
  //   next()
  // }
})
router.afterEach(() => {
  NProgress.done()
})
export default router
